<?php
/*
 * database config reader and manipulater
 *
 * to increase useability of this file:
 * rename to tools.php or helpers.php and use it as a general toolbox.
 * also change the 'readDBConnectConf()' file to use an
 * associative array which it returns instead of a switch,
 * taking filename as an argument,
 * that way it can be used to read any config file.
 */
 /*
  * this function is probably not safe for production systems
  *
  * it reads a named config file from working root
  * and puts all values into its corresponding
  * variables, while at the same time skipping
  * comments
  *
  *=================CONFIGFILE====================
  * # entry syntax is as follows:
  * # categorytoseparatevaluesinswitch=valuetobeassignedinswitch
  * # there should not be any whitespaces
  * # in these lines as the reader function
  * # may not be able to read the values
  * # correctly
  * #
  * # all comments start with a hash,
  * # and is thereby supposed to be ignored
  * DBhost=hitpoints
  * DBuser=eebs
  * DBpass=eebs database password
  *===============================================
  *
  */
	function readDBConnectConf()
	{
		$configfile = "eebsconf.ini";
		$configfilehandle = fopen($configfile, 'r')
			or die("cannot open file " . $configfile);
		while ($currdatacat = fgets($configfilehandle) != false) {
			$currdatacat = trim($currdatacat);
			$currdatacat = rtrim($currdatacat);
			$commentstart = strpos($currdatacat, '#');
			if ($commentstart == 0) { // this should not do anything
			// because if the first character is a hash (#) the entire
			// line is a coment and should just be skipped.
			} else {
				$currdatacat = substr($currdatacat, 0, $commentstart);
				$currdata = trim(strstr($currdatacat, '=', false), '=');
				$currdatacat = strstr($currdatacat, '=', true);
				echo $currdatacat . "+" . $currdata;
				switch($currdatacat) {
					// add in new entries here as new
					// entries are put into the
					// configuration file
					case "DBhost":
						global $DBHOST;
						$DBHOST = $currdata;
						echo $DBHOST;
						break;
					case "DBuser":
						global $DBUSER;
						$DBUSER = $currdata;
						echo $DBUSER;
						break;
					case "DBpass":
						global $DBPASS;
						$DBPASS = $currdata;
						echo $DBPASS;
						break;
					case "DBname":
						global $DBNAME;
						$DBNAME = $currdata;
						echo $DBNAME;
						break;
					default:
						echo "no configuration
							parameter matching: "
							. $currdatacat .
							" with value: " .
							$currdata;
						break;
				}
			}
		}
		if (!feof($configfilehandle)) {
			echo "fgets error reading file: " . $configfile;
		}
		fclose($configfilehandle);
	}
 ?>
